﻿using LPBToolsNF.Properties;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LPBToolsNF
{
    public class SvgInfo
    {
        [JsonIgnore]
        public const string JCStr_Path = "path";
        [JsonIgnore]
        public const string JCStr_ViewBox = "viewBox";
        [JsonIgnore]
        //public const string MDSvgMask = "<svg width=\"{0}\" height=\"{1}\" viewBox=\"0 0 {2}\" fill=\"{3}\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"100%\" height=\"100%\" fill=\"{7}\" /><path d=\"{4}\" stroke=\"{5}\" stroke-width=\"{6}\" stroke-linecap=\"round\" stroke-linejoin=\"round\"/></svg>";
        public const string MDSvgMask = "<svg width=\"{0}\" height=\"{1}\" viewBox=\"0 0 {2}\" xmlns=\"http://www.w3.org/2000/svg\" style=\"background: {6};\"><rect width=\"100%\" height=\"100%\" fill=\"{6}\" /><path d=\"{3}\" stroke=\"{4}\" stroke-width=\"{5}\" stroke-linecap=\"round\" stroke-linejoin=\"round\" fill=\"{4}\" /></svg>";
        [JsonProperty(JCStr_Path)]
        public string path { get; set; }
        [JsonProperty(JCStr_ViewBox)]
        public string viewBox { get; set; }

        public int Width
        {
            get
            {
                int iWidth = 16;
                if ((!string.IsNullOrEmpty(viewBox)) && (viewBox.IndexOf(" ") > 0))
                {
                    string sWidth = viewBox.Substring(0, viewBox.IndexOf(" "));
                    int.TryParse(sWidth, out iWidth);
                }
                return iWidth;
            }
        }
        public int Height
        {
            get
            {
                int iHeight = 16;
                if ((!string.IsNullOrEmpty(viewBox)) && (viewBox.IndexOf(" ") > 0))
                {
                    string sHeight = viewBox.Substring(viewBox.IndexOf(" ") + 1);
                    int.TryParse(sHeight, out iHeight);
                }
                return iHeight;
            }
        }

        /// <summary>
        /// <svg width="16" height="16" viewBox="0 0 24 24" fill="none" xmlns=\"http://www.w3.org/2000/svg\"><path d="M12 2L2 7l10 5 10-5-10-5zM2 17l10 5 10-5M2 12l10 5 10-5" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>
        /// </summary>
        /// <param name="Path"></param>
        /// <param name="ViewBox"></param>
        /// <param name="iwidth"></param>
        /// <param name="iHeight"></param>
        /// <param name="Fill"></param>
        /// <param name="Stroke"></param>
        /// <param name="StrokeWidth"></param>
        /// <returns></returns>
        public string getSvg(string ViewBox, int iwidth = 16, int iHeight = 16, string Fill = "none", string backColor = "white", string StrokeWidth = "2")
        {
            return string.Format(MDSvgMask, iwidth, iHeight, ViewBox, path, Fill, StrokeWidth, backColor);
        }

        public string getSvg(string svgColor, string svgBackColor = "White", int iwidth = 16, int iHeight = 16)
        {
            return getSvg(viewBox, iwidth, iHeight, svgColor, svgBackColor, "2");
        }
    }

    public class SvgICONTool
    {
        public const string SvgSegName_brands = "svg_brands";
        public const string SvgSegName_solid = "svg_solid";
        public const string SvgSegName_regular = "svg_regular";

        public static string[] SvgSegList { get { return new string[] { SvgSegName_brands, SvgSegName_solid, SvgSegName_regular }; } }

        protected Dictionary<string, SvgInfo> svgDict = new Dictionary<string, SvgInfo>();
        public SvgICONTool() : this(SvgSegName_regular)
        {
        }
        public SvgICONTool(string svgResName)
        {
            string svgJSON = "";
            switch (svgResName)
            {
                case "svg_brands":
                    svgJSON = Resources.svg_brands;
                    break;
                case "svg_solid":
                    svgJSON = Resources.svg_solid;
                    break;
                case "svg_regular":
                    svgJSON = Resources.svg_regular;
                    break;
            }
            if (!string.IsNullOrEmpty(svgJSON))
            {
                try
                {
                    svgDict = JsonConvert.DeserializeObject<Dictionary<string, SvgInfo>>(svgJSON);
                }
                catch (Exception ex)
                { }
            }
        }

        public string[] svgList { get { return svgDict.Keys.ToArray(); } }

        /// <summary>
        /// 获得图标的原始尺寸
        /// </summary>
        /// <param name="svgName"></param>
        /// <param name="iwidth"></param>
        /// <param name="iHeight"></param>
        /// <returns></returns>
        public bool getSveSize(string svgName, out int iwidth, out int iHeight)
        {
            iwidth = 16;
            iHeight = 16;
            if (svgDict.ContainsKey(svgName))
            {
                iwidth = svgDict[svgName].Width;
                iHeight = svgDict[svgName].Height;
                return true;
            }
            return false;
        }

        /// <summary>
        /// 获得图标的信息字符串
        /// </summary>
        /// <param name="svgStr">返回Svg图标字符串</param>
        /// <param name="svgName">Svg图标名称</param>
        /// <param name="svgColor">Svg图标线条颜色</param>
        /// <param name="svgBackColor">Svg图标背景颜色</param>
        /// <param name="iwidth">Svg图标宽，输入0则使用图标原始宽度</param>
        /// <param name="iHeight">Svg图标高，输入0则使用图标原始高度</param>
        /// <returns></returns>
        public bool getSvg(out string svgStr, string svgName, string svgColor, string svgBackColor, int iwidth = 16, int iHeight = 16)
        {
            svgStr = "";
            if (svgDict.ContainsKey(svgName))
            {
                int iw = (iwidth <= 0) ? svgDict[svgName].Width : iwidth;
                int ih = (iHeight <= 0) ? svgDict[svgName].Height : iHeight;

                svgStr = svgDict[svgName].getSvg(svgColor, svgBackColor, iw, ih);
            }
            return !string.IsNullOrEmpty(svgStr);
        }
    }
}
